<?php

class Service_weixin extends MY_Service
{

    public function send()
    {
        // 设置必要的参数，如公众号AppID、AppSecret、文章标题、正文内容等
        $appid = 'your_appid';
        $secret = 'your_secret';
        $title = 'your_article_title';
        $content = 'your_article_content';
        $picurl = 'your_article_pic_url';

        // 获取access_token
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
        $result = file_get_contents($url);
        $access_token = json_decode($result)->access_token;

        // 上传图片素材并获取media_id
        $picdata = array("media" => new CURLfile(realpath($picurl)));
        $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=" . $access_token . "&type=image";
        $result = json_decode(postData($url, $picdata));
        $media_id = $result->media_id;

        // 组装图文消息素材
        $data = array(
            "articles" => array(
                array(
                    "title" => $title,
                    "thumb_media_id" => $media_id, // 封面图片的media_id
                    "author" => "your_name",
                    "digest" => mb_substr($content, 0, 54), // 摘要不超过54个字符
                    "show_cover_pic" => 1,
                    "content" => $content,
                    "content_source_url" => "http://www.example.com" // 原文链接（可选）
                )
            )
        );

        // 发送图文消息
        $url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=" . $access_token;
        $result = json_decode(postData($url, $data));

        // 打印结果
        var_dump($result);

        // 发送POST请求
        function postData($url, $data)
        {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            $result = curl_exec($curl);
            curl_close($curl);
            return $result;
        }
    }
}